<script setup>
import Img from '@/assets/img.png'
import {reactive} from "vue";
import {sendcode,reset} from "@/api/reset.js";
import {useRouter} from "vue-router";
const router = useRouter()
const formState = reactive({
  email: '',
  newPassword: '',
  verificationCode: ''
})
async function sendCode(){
  const res = await sendcode(formState.email);
  if (res.data.code === 200) {
    alert("验证码发送成功")
  } else {
    alert("验证码发送失败")
  }
}
async function Reset(){
  const res = await reset(formState.email,formState.verificationCode,formState.newPassword);
  if (res.data.code === 200) {
    alert("重置成功")
    router.push('/login');
  } else {
    alert("重置失败")
  }
}
</script>

<template>
<div class="reset-container">
 <div class = "image-container" >
   <img :src="Img" class = "background-image"/>
 </div>
  <div class="form-wrapper">
    <div class="form-header">
      <h1>Reset Password</h1>
      <p>Please enter your new credentials</p>
    </div>
    <a-form
        :model="formState"
        name="normal_login"
        class="auth-form"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
    >
      <a-form-item
          class="form-item"
          label="Email"
          name="email"
          :rules="[{ required: true, message: 'Please input your email!' }]"
      >
        <a-input v-model:value="formState.email">
          <template #prefix>
            <MailOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item
          class="form-item"
          label="Verification"
          name="verification"
          :rules="[{ required: true, message: 'Please input your verification!' }]"
      >
        <a-input-group compact>
          <a-input
              v-model:value="formState.verificationCode"
              style="width: calc(100% - 100px)"
          >
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input>
          <a-button type="primary" style="width: 100px" @click="sendCode">Send</a-button>
        </a-input-group>
      </a-form-item>
      <a-form-item
          class="form-item"
          label="Password"
          name="password"
          :rules="[{ required: true, message: 'Please input your password!' }]"
      >
        <a-input-password v-model:value="formState.newPassword">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>
      <a-form-item>
        <a-button :disabled="disabled" type="primary" html-type="submit"  class="submit-btn" @click="Reset">
          Reset Password
        </a-button>
      </a-form-item>
    </a-form>
    <div class="additional-links">
      <a href="/login">← Back to Login</a>
    </div>
  </div>
</div>
</template>

<style scoped>
.reset-container {
  display: flex;
  min-height: 100vh;
  background: #ffffff;
}

.background-layer {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #f0f2f5; /* 添加深色背景提高图片对比度 */
}

.background-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.9;
  filter: grayscale(10%); /* 可选：轻微灰度保持专业感 */
}

.form-wrapper {
  width: 100%;
  max-width: 480px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h1 {
  font-size: 28px;
  color: #2d3748;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-header p {
  color: #718096;
  font-size: 14px;
}

.auth-form {
  :deep(.ant-input-affix-wrapper) {
    border: 1px solid #d9d9d9 !important;
    border-radius: 6px;
    padding: 8px 11px;
    transition: all 0.3s;

    &:hover {
      border-color: #4096ff !important;
    }

    &:focus-within {
      border-color: #4096ff !important;
      box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2) !important;
    }
  }

  :deep(.ant-input-password) {
    border: 1px solid #d9d9d9 !important;
    border-radius: 6px;
    padding: 8px 11px;

    .ant-input {
      border: none !important;
      box-shadow: none !important;
    }
  }

  :deep(.ant-input-group) {
    display: flex;

    .ant-input {
      border-top-right-radius: 0 !important;
      border-bottom-right-radius: 0 !important;
    }

    .ant-btn {
      border-top-left-radius: 0 !important;
      border-bottom-left-radius: 0 !important;
      height: 40px; /* 保持与输入框高度一致 */
    }
  }
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  transition: all 0.3s;
  border: none;

  &:not([disabled]):hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(118, 75, 162, 0.3);
  }

  &[disabled] {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

.additional-links {
  text-align: center;
  margin-top: 24px;

  a {
    color: #667eea;
    text-decoration: none;
    font-weight: 500;
    transition: color 0.3s;

    &:hover {
      color: #764ba2;
    }
  }
}

@media (max-width: 768px) {
  .reset-container {
    flex-direction: column;
  }

  .background-layer {
    height: 200px;
  }

  .form-wrapper {
    max-width: 100%;
    margin-top: -60px;
    border-radius: 24px 24px 0 0;
  }
}

</style>